package com.yuejian_06.yuejian_06.fragment;


import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.Toast;

import com.yuejian_06.yuejian_06.Listener.LoginListener;
import com.yuejian_06.yuejian_06.R;
import com.yuejian_06.yuejian_06.activity.CourseSearchActivity;
import com.yuejian_06.yuejian_06.adapter.CourseAdapter;
import com.yuejian_06.yuejian_06.bean.CourseBean;
import com.yuejian_06.yuejian_06.model.CourseModel;

import java.util.List;

/**
 * A simple {@link Fragment} subclass.
 */
public class CourseFragment extends Fragment {
    RecyclerView recyclerView;
    Button btn_star1,btn_star2,btn_star3,btn_all;
    ImageButton btn_search;

    private LoginListener<List<CourseBean>> Listener=new LoginListener<List<CourseBean>>() {
        @Override
        public void onResponse(List<CourseBean> o) {
            List<CourseBean> mDataList=o;
            recyclerView.setAdapter(new CourseAdapter(getActivity(),mDataList));//设置适配器
        }
        @Override
        public void onFail(String msg) {
            Toast.makeText(getActivity().getApplicationContext(),"失败了",Toast.LENGTH_SHORT).show();

        }
    };

    public CourseFragment() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_course, container, false);
        btn_all=(Button) view.findViewById(R.id.course_btnall);
        btn_star1=(Button) view.findViewById(R.id.course_btn1);
        btn_star2=(Button) view.findViewById(R.id.course_btn2);
        btn_star3=(Button) view.findViewById(R.id.course_btn3);
        btn_search=(ImageButton) view.findViewById(R.id.course_search);
        recyclerView = (RecyclerView) view.findViewById(R.id.course_rv);
        recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));// 布局
        initViews();
        initEvents();
        return view;
    }


    private void initViews() {
        CourseModel courseModel=new CourseModel();
        courseModel.courseList(Listener);
    }

    private void initEvents(){
        //进入课程搜索页面
        btn_search.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getActivity(), CourseSearchActivity.class);
                startActivity(intent);
            }
        });

        btn_all.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                btn_star1.setTextColor(Color.parseColor("#000000"));
                btn_all.setTextColor(Color.parseColor("#169bd5"));
                btn_star2.setTextColor(Color.parseColor("#000000"));
                btn_star3.setTextColor(Color.parseColor("#000000"));
                initViews();
            }
        });
        btn_star1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                btn_star1.setTextColor(Color.parseColor("#169bd5"));
                btn_all.setTextColor(Color.parseColor("#000000"));
                btn_star2.setTextColor(Color.parseColor("#000000"));
                btn_star3.setTextColor(Color.parseColor("#000000"));
                CourseModel courseModel=new CourseModel();
                courseModel.getcourseStar("1",Listener);
            }
        });
        btn_star2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                btn_star1.setTextColor(Color.parseColor("#000000"));
                btn_all.setTextColor(Color.parseColor("#000000"));
                btn_star2.setTextColor(Color.parseColor("#169bd5"));
                btn_star3.setTextColor(Color.parseColor("#000000"));
                CourseModel courseModel=new CourseModel();
                courseModel.getcourseStar("2",Listener);
            }
        });
        btn_star3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                btn_star1.setTextColor(Color.parseColor("#000000"));
                btn_all.setTextColor(Color.parseColor("#000000"));
                btn_star2.setTextColor(Color.parseColor("#000000"));
                btn_star3.setTextColor(Color.parseColor("#169bd5"));
                CourseModel courseModel=new CourseModel();
                courseModel.getcourseStar("3",Listener);
            }
        });

    }

}
